package com.rockcent.api.controller;

import com.rockcent.api.Routes;
import com.rockcent.common.common.JsonResult;
import com.rockcent.common.common.RestDoing;
import com.rockcent.common.controller.BaseController;
import com.rockcent.mall.lib.domain.MiniAppConfig;
import com.rockcent.mall.lib.dto.MiniAppConfigDto;
import com.rockcent.mall.lib.dto.UserInfoDto;
import com.rockcent.mall.lib.helper.BaseUserHelper;
import com.rockcent.mall.lib.repository.MiniAppConfigRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by marco on 2017/7/11.
 */
@RestController
@RequestMapping(value = Routes.API_VERSION)
public class MallMiniAppConfigController extends BaseController {

    private final Logger log = LoggerFactory.getLogger(MallMiniAppConfigController.class);

    @Autowired
    private BaseUserHelper userHelper;
    @Autowired
    private MiniAppConfigRepository miniAppConfigRepository;

    /**
     * @api {post} /v1/mall/miniapp/config/save M 小程序配置保存
     * @apiGroup mall_miniapp
     * @apiVersion 1.0.0
     * @apiHeader {String} userToken 用户token
     * @apiHeader {String} domain    请求域名
     * @apiHeader {String} platformMark    平台类型
     *
     * @apiParam {String} appId <code>必须参数</code> 小程序ID
     * @apiParam {String} appSecret <code>必须参数</code> 小程序密钥
     * @apiParam {String} mchId <code>必须参数</code> 商户号
     * @apiParam {String} mchSecret <code>必须参数</code> 商户密钥
     * @apiParam {String} templateId <code>必须参数</code> 模板ID
     *
     * @apiSuccess {Long} id 规则ID
     * @apiSuccess {Long} mallId 平台ID
     * @apiSuccess {String} appId 小程序ID
     * @apiSuccess {String} appSecret 小程序密钥
     * @apiSuccess {String} mchId 商户号
     * @apiSuccess {String} mchSecret 商户密钥
     *
     * @apiSuccessExample {json} Success-Response:
     *
     * {
     *   "code": "1",
     *   "desc": "成功",
     *   "data": {
     *     "id": 1,
     *     "version": 0,
     *     "mallId": 51,
     *     "appId": "123",
     *     "appSecret": "1232356",
     *     "mchId": "wrww13123",
     *     "mchSecret": "rwerer566"
     *   }
     * }
     *
     */
    @RequestMapping(value = Routes.MALL_MINIAPP_CONFIG_SAVE, method = RequestMethod.POST)
    public JsonResult save(@RequestBody MiniAppConfigDto miniAppConfigDto, HttpServletRequest request) {
        UserInfoDto userDto = userHelper.getSession(request);
        RestDoing doing = jsonResult -> {
            if (userDto.mall == null || userDto.mall.getId() == null) {
                jsonResult.remind("平台id 不能为空", log);
                return;
            }
            if (miniAppConfigDto.getAppId() == null) {
                jsonResult.remind("appId 不能为空", log);
                return;
            }
            if (miniAppConfigDto.getAppSecret() == null) {
                jsonResult.remind("appSecret 不能为空", log);
                return;
            }
            if (miniAppConfigDto.getMchId() == null) {
                jsonResult.remind("mchId 不能为空", log);
                return;
            }
            if (miniAppConfigDto.getMchSecret() == null) {
                jsonResult.remind("mchSecret 不能为空", log);
                return;
            }
            if (miniAppConfigDto.getTemplateId() == null) {
                jsonResult.remind("templateId 不能为空", log);
                return;
            }
            MiniAppConfig miniAppConfig = miniAppConfigRepository.findByMallId(userDto.mall.getId());
            if(miniAppConfig == null) {
                miniAppConfig = new MiniAppConfig();
            }
            miniAppConfig.setMallId(userDto.mall.getId());
            miniAppConfig.setAppId(miniAppConfigDto.getAppId());
            miniAppConfig.setAppSecret(miniAppConfigDto.getAppSecret());
            miniAppConfig.setMchId(miniAppConfigDto.getMchId());
            miniAppConfig.setMchSecret(miniAppConfigDto.getMchSecret());
            miniAppConfig.setTemplateId(miniAppConfigDto.getTemplateId());
            jsonResult.data = miniAppConfigRepository.save(miniAppConfig);
        };
        return doing.go(request, log);
    }

    /**
     * @api {get} /v1/mall/miniapp/config/info M 小程序配置详情
     * @apiGroup mall_miniapp
     * @apiVersion 1.0.0
     * @apiHeader {String} userToken 用户token
     * @apiHeader {String} domain    请求域名
     * @apiHeader {String} platformMark    平台类型
     *
     * @apiSuccess {Long} id 规则ID
     * @apiSuccess {Long} mallId 平台ID
     * @apiSuccess {String} appId 小程序ID
     * @apiSuccess {String} appSecret 小程序密钥
     * @apiSuccess {String} mchId 商户号
     * @apiSuccess {String} mchSecret 商户密钥
     *
     * @apiSuccessExample {json} Success-Response:
     *
     * {
     *   "code": "1",
     *   "desc": "成功",
     *   "data": {
     *     "id": 1,
     *     "version": 0,
     *     "mallId": 51,
     *     "appId": "123",
     *     "appSecret": "1232356",
     *     "mchId": "wrww13123",
     *     "mchSecret": "rwerer566"
     *   }
     * }
     *
     */
    @RequestMapping(value = Routes.MALL_MINIAPP_CONFIG_INFO, method = RequestMethod.GET)
    public JsonResult info(HttpServletRequest request) {
        UserInfoDto userDto = userHelper.getSession(request);
        RestDoing doing = jsonResult -> {
            if (userDto.mall == null || userDto.mall.getId() == null) {
                jsonResult.remind("平台id 不能为空", log);
                return;
            }
            MiniAppConfig miniAppConfig = miniAppConfigRepository.findByMallId(userDto.mall.getId());
            if(miniAppConfig == null) {
                miniAppConfig = new MiniAppConfig();
            }
            jsonResult.data = miniAppConfig;
        };
        return doing.go(request, log);
    }

    /**
     * @api {get} /v1/merchant/miniapp/config/info B 小程序配置详情
     * @apiGroup merchant_miniapp
     * @apiVersion 1.0.0
     * @apiHeader {String} userToken 用户token
     * @apiHeader {String} domain    请求域名
     * @apiHeader {String} platformMark    平台类型
     *
     * @apiSuccess {Long} id 规则ID
     * @apiSuccess {Long} mallId 平台ID
     * @apiSuccess {String} appId 小程序ID
     * @apiSuccess {String} appSecret 小程序密钥
     * @apiSuccess {String} mchId 商户号
     * @apiSuccess {String} mchSecret 商户密钥
     *
     * @apiSuccessExample {json} Success-Response:
     *
     * {
     *   "code": "1",
     *   "desc": "成功",
     *   "data": {
     *     "id": 1,
     *     "version": 0,
     *     "mallId": 51,
     *     "appId": "123",
     *     "appSecret": "1232356",
     *     "mchId": "wrww13123",
     *     "mchSecret": "rwerer566"
     *   }
     * }
     *
     */
    @RequestMapping(value = Routes.MERCHANT_MINIAPP_CONFIG_INFO, method = RequestMethod.GET)
    public JsonResult config(HttpServletRequest request) {
        UserInfoDto userInfoDto = userHelper.getSession(request);
        RestDoing doing = jsonResult -> {
            if (userInfoDto.mall == null || userInfoDto.mall.getId() == null) {
                jsonResult.remind("平台id 不能为空", log);
                return;
            }
            MiniAppConfig miniAppConfig = miniAppConfigRepository.findByMallId(userInfoDto.mall.getId());
            if(miniAppConfig == null) {
                miniAppConfig = new MiniAppConfig();
            }
            jsonResult.data = miniAppConfig;
        };
        return doing.go(request, log);
    }
}
